package com.swxy.club.service.activity;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swxy.club.dto.Activity;
import com.swxy.club.dto.ActivityMember;
import com.swxy.club.dto.CheckinRecord;
import com.swxy.club.dto.bo.CurrentUserBO;
import com.swxy.club.enums.CheckinStatus;
import com.swxy.club.enums.RegistrationStatus;
import com.swxy.club.mapper.ActivityMemberMapper;
import com.swxy.club.service.record.CheckinRecordService;
import com.swxy.club.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author 易
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ActivityMemberServiceImpl extends ServiceImpl<ActivityMemberMapper, ActivityMember> implements ActivityMemberService{

    private final CheckinRecordService checkinRecordService;
    private final ActivityService activityService;


    @Override
    @Transactional
    public void joinActivity(Integer activityId) {

        CurrentUserBO currentUser = UserContext.getCurrentUser();
        Assert.notNull(currentUser, "用户未登入");

        Activity activity = activityService.getActivityById(activityId);
        Assert.notNull(activity, "活动不存在");

        Assert.isTrue(activity.getCurrentNumber() < activity.getMaxNumber(), "活动人数已满");

        Assert.isTrue(activity.getStartTime().isAfter(LocalDateTime.now()), "活动已结束");

        ActivityMember member = getOne(new QueryWrapper<ActivityMember>()
                .lambda()
                .eq(ActivityMember::getActivityId, activityId)
                .eq(ActivityMember::getUserId, currentUser.getId()));

        Assert.isTrue(member == null || member.getStatus() != RegistrationStatus.REGISTERED, "您已经报名了该活动");

        ActivityMember activityMember = new ActivityMember();
        activityMember.setActivityId(Long.valueOf(activityId));
        activityMember.setUserId(currentUser.getId());
        activityMember.setStatus(RegistrationStatus.REGISTERED);
        activityMember.setRegisterTime(LocalDateTime.now());

        this.save(activityMember);

        activity.setCurrentNumber(activity.getCurrentNumber() + 1);
        activityService.updateById(activity);


        CheckinRecord record = new CheckinRecord();
        record.setActivityId(Long.valueOf(activityId));
        record.setUserId(currentUser.getId());
        record.setStatus(CheckinStatus.ABSENT);
        checkinRecordService.addCheckinRecord(record);

    }

    @Override
    public List<Activity> getCurrentUserActivity() {

        CurrentUserBO currentUser = UserContext.getCurrentUser();
        Assert.notNull(currentUser, "用户未登入");

       // 根据用户id查询所有的活动id
        List<Long> activityIds = this.listObjs(new LambdaQueryWrapper<ActivityMember>()
                        .eq(ActivityMember::getUserId, currentUser.getId())
                        .select(ActivityMember::getActivityId),
                obj -> (Long) obj);
        if (!activityIds.isEmpty()) {
            return activityService.listByIds(activityIds);
        }
        return List.of();
    }
}
